In This Topic
Programming / PDF / Filling forms in a PDF document, flattening them and saving the result

Filling forms in a PDF document, flattening them and saving the result

In This Topic

The goal of this example is to open a fillable PDF document, fill-in forms, flatten the document and save the result with a different name, keeping the original form untouched.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.

Dim oGdPicturePDF As New GdPicturePDF()

'Loading the PDF document.

Dim status As GdPictureStatus = oGdPicturePDF.LoadFromFile("Form.pdf", False)

If status <> GdPictureStatus.OK Then

    MessageBox.Show("The PDF document can't be loaded. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

    GoTo [end]

End If

'Getting the number of form fields.

status = GdPictureStatus.OK

Dim FieldCount As Integer = oGdPicturePDF.GetFormFieldsCount()

For x As Integer = 0 To FieldCount - 1

    'Getting the field identifier.

    Dim FormFieldId As Integer = oGdPicturePDF.GetFormFieldId(x)

    status = oGdPicturePDF.GetStat()

    If status = GdPictureStatus.OK Then

        'Getting the field title.

        Dim FormFieldTitle As String = oGdPicturePDF.GetFormFieldTitle(FormFieldId)

        status = oGdPicturePDF.GetStat()

        If status = GdPictureStatus.OK Then

            'Using the field title to put the text into the form field.

            Select Case FormFieldTitle

                Case "Name"

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "Doe")

                    Exit Select

                Case "Nickname"

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "John")

                    Exit Select

                Case "COI"

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "Tennis, Swimming, Golf, SF books")

                    Exit Select

            End Select

        Else

            Exit For

        End If

    Else

        Exit For

    End If

Next

If status = GdPictureStatus.OK Then

    'Flattening forms.

    status = oGdPicturePDF.FlattenFormFields()

    If status <> GdPictureStatus.OK Then

        MessageBox.Show("Error occurred when flatenning forms. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        GoTo [end]

    End If

    'Saving the result.

    status = oGdPicturePDF.SaveToFile("Form_filled.pdf")

    If status <> GdPictureStatus.OK Then

        MessageBox.Show("The PDF document can't be saved. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

    Else

        MessageBox.Show("Done!", "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End If

Else

    MessageBox.Show("Error occurred when filling forms. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

[end]:

oGdPicturePDF.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.

GdPicturePDF oGdPicturePDF = new GdPicturePDF();

//Loading the PDF document.

GdPictureStatus status = oGdPicturePDF.LoadFromFile("Form.pdf", false);

if (status != GdPictureStatus.OK)

{

    MessageBox.Show("The PDF document can't be loaded. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

    goto end;

}

//Getting the number of form fields.

status = GdPictureStatus.OK;

int FieldCount = oGdPicturePDF.GetFormFieldsCount();

for (int x = 0; x <= FieldCount - 1; x++)

{

    //Getting the field identifier.

    int FormFieldId = oGdPicturePDF.GetFormFieldId(x);

    status = oGdPicturePDF.GetStat();

    if (status == GdPictureStatus.OK)

    {

        //Getting the field title.

        string FormFieldTitle = oGdPicturePDF.GetFormFieldTitle(FormFieldId);

        status = oGdPicturePDF.GetStat();

        if (status == GdPictureStatus.OK)

        {

            //Using the field title to put the text into the form field.

            switch (FormFieldTitle)

            {

                case "Name":

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "Doe");

                    break;

                case "Nickname":

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "John");

                    break;

                case "COI":

                    oGdPicturePDF.SetFormFieldValue(FormFieldId, "Tennis, Swimming, Golf, SF books");

                    break;

            }

        }

        else

            break;

    }

    else

        break;

}

if (status == GdPictureStatus.OK)

{

    //Flattening forms.

    status = oGdPicturePDF.FlattenFormFields();

    if (status != GdPictureStatus.OK)

    {

        MessageBox.Show("Error occurred when flatenning forms. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        goto end;

    }

    //Saving the result.

    status = oGdPicturePDF.SaveToFile("Form_filled.pdf");

    if (status != GdPictureStatus.OK)

    {

        MessageBox.Show("The PDF document can't be saved. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    else

        MessageBox.Show("Done!", "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

    MessageBox.Show("Error occurred when filling forms. Error: " + status.ToString(), "Filling Forms Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

} 

end:

oGdPicturePDF.Dispose();